package com.snail.cloud.resp;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 统一返回结果对象
 *
 * @author snail
 * @date 2024/9/23
 */
@Data
@Accessors(chain = true)
public class ResultData<T> {
    /**
     * 状态码
     */
    private int code;

    /**
     * 状态码描述信息
     */
    private String message;
    /**
     * 数据
     */
    private T data;
    /**
     * 访问时间戳
     */
    private long timestamp;


    public ResultData() {
        this.timestamp = System.currentTimeMillis();
    }

    /**
     * 获取统一返回结果信息，成功
     *
     * @param data 数据
     * @return {@link ResultData }<{@link T }>
     * @author Snail
     * @date 2024/09/23
     */
    public static <T> ResultData<T> success(T data) {
        ResultData resultData = new ResultData<>();

        resultData.setCode(ReturnCodeEnum.RC200.getCode());
        resultData.setMessage(ReturnCodeEnum.RC200.getMessage());
        resultData.setData(data);

        return resultData;
    }

    /**
     * 获取统一返回结果信息,失败
     *
     * @param code    代码
     * @param message 消息
     * @return {@link ResultData }<{@link T }>
     * @author Snail
     * @date 2024/09/23
     */
    public static <T> ResultData<T> fail(Integer code, String message) {
        ResultData resultData = new ResultData<>();

        resultData.setCode(code);
        resultData.setMessage(message);
        resultData.setData(null);

        return resultData;
    }
}
